//
//  LoveView.m
//  tcpoctest
//
//  Created by apple on 13-8-2.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "LoveView.h"
#import "sendTelpGetUser.h"

#import "TelepathyDataSource.h"
#import "bigHeadImageDataSource.h"
#import "BigImageInfo.h"
#import "DPMeterView.h"
#import "DPMeterView.h"
#import "UIBezierPath+BasicShapes.h"
#import "sendTelepCandle.h"
#import "sendTelepLight.h"
#import "UIDevice_Jailbroken.h"

@implementation LoveView

enum LVGameState
{
    LVS_WAIT,
    LVS_START,
    LVS_WAIT_LIGHT,
};

enum LVAlertType
{
    LVAT_LIGHT,
    LVAT_LIGHTFAIL,
};

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    UIImageView *topview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 60)];
    UIImage  *backgroudImage = [UIImage imageNamed:@"tele_topbar.png"];
    topview.backgroundColor=[UIColor colorWithPatternImage:backgroudImage];
    [self.view addSubview:topview];
    
    UILabel *toblabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 60)];
    toblabel.backgroundColor = [UIColor clearColor];
    toblabel.textAlignment = UITextAlignmentCenter;
    toblabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:21.0];
    toblabel.text = @"一见钟情";
    toblabel.textColor = [UIColor whiteColor];
    [self.view addSubview:toblabel];
    
    UIButton *backbtn = [[UIButton alloc] initWithFrame:CGRectMake(4, 16, 40, 25)];
    [backbtn setImage:[UIImage imageNamed:@"tele_backbtn.png"] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(backBtnPress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backbtn];
    
    headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, WINDOW_WIDTH, WINDOW_HIGTH - 160)];
    [self.view addSubview:headview];
    
    UILabel *backgblabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 64, 80, 80)];
    backgblabel.backgroundColor = [UIColor blackColor];
    backgblabel.alpha = 0.498;
    [self.view addSubview:backgblabel];
    
    UIImageView *coinv = [[UIImageView alloc] initWithFrame:CGRectMake(6, 68, 20, 20)];
    coinv.image = [UIImage imageNamed:@"tele_gold.png"];
    [self.view addSubview:coinv];
    
    UIImageView *candlev = [[UIImageView alloc] initWithFrame:CGRectMake(6, 112, 20, 20)];
    candlev.image = [UIImage imageNamed:@"tele_light_count.png"];
    [self.view addSubview:candlev];
    
    UIImageView *progressv = [[UIImageView alloc] initWithFrame:CGRectMake(0, WINDOW_HIGTH - 160, WINDOW_WIDTH, 80)];
    progressv.image = [UIImage imageNamed:@"tele_bg.png"];
    [self.view addSubview:progressv];
    
    UIImageView *bottomv = [[UIImageView alloc] initWithFrame:CGRectMake(0, WINDOW_HIGTH - 80, WINDOW_WIDTH, 80)];
    backgroudImage = [UIImage imageNamed:@"tele_bottombar.png"];
    bottomv.backgroundColor=[UIColor colorWithPatternImage:backgroudImage];
    [self.view addSubview:bottomv];
    
    UIButton *lightbtn = [[UIButton alloc] initWithFrame:CGRectMake(65, WINDOW_HIGTH - 80, 30, 30)];
    [lightbtn setImage:[UIImage imageNamed:@"tele_light.png"] forState:UIControlStateNormal];
    [lightbtn addTarget:self action:@selector(lightbtnpress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:lightbtn];
    
    UIButton *giveupbtn = [[UIButton alloc] initWithFrame:CGRectMake(225, WINDOW_HIGTH - 80, 30, 30)];
    [giveupbtn setImage:[UIImage imageNamed:@"tele_give_up.png"] forState:UIControlStateNormal];
    [giveupbtn addTarget:self action:@selector(giveupbtnpress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:giveupbtn];
    
    UILabel *instepLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WINDOW_HIGTH - 152, 60, 15)];
    instepLabel.backgroundColor = [UIColor clearColor];
    instepLabel.text = @"合拍度";
    instepLabel.textColor = [UIColor whiteColor];
    instepLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:instepLabel];
    
    UILabel *opportunityLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WINDOW_HIGTH - 130, 60, 15)];
    opportunityLabel.backgroundColor = [UIColor clearColor];
    opportunityLabel.text = @"机缘度";
    opportunityLabel.textColor = [UIColor whiteColor];
    opportunityLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:opportunityLabel];
    
    UILabel *matchLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WINDOW_HIGTH - 108, 60, 15)];
    matchLabel.backgroundColor = [UIColor clearColor];
    matchLabel.text = @"匹配度";
    matchLabel.textColor = [UIColor whiteColor];
    matchLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:matchLabel];
    
    [[DPMeterView appearance] setTrackTintColor:[UIColor lightGrayColor]];
    [[DPMeterView appearance] setProgressTintColor:[UIColor darkGrayColor]];
    
    shapeStepView = [[DPMeterView alloc] initWithFrame:CGRectMake(80, WINDOW_HIGTH - 152, 100, 15)];
    [shapeStepView setMeterType:DPMeterTypeLinearHorizontal];
    [shapeStepView setShape:[UIBezierPath stars:5 shapeInFrame:shapeStepView.frame].CGPath];
    shapeStepView.progressTintColor = [UIColor colorWithRed:255/255.f green:199/255.f blue:87/255.f alpha:1.f];
    shapeStepView.trackTintColor = [UIColor grayColor];
    [self.view addSubview:shapeStepView];
    
    shapeOppView = [[DPMeterView alloc] initWithFrame:CGRectMake(80, WINDOW_HIGTH - 130, 100, 15)];
    [shapeOppView setMeterType:DPMeterTypeLinearHorizontal];
    [shapeOppView setShape:[UIBezierPath stars:5 shapeInFrame:shapeOppView.frame].CGPath];
    shapeOppView.progressTintColor = [UIColor colorWithRed:255/255.f green:199/255.f blue:87/255.f alpha:1.f];
    shapeOppView.trackTintColor = [UIColor grayColor];
    [self.view addSubview:shapeOppView];
    
    shapeMatchView = [[DPMeterView alloc] initWithFrame:CGRectMake(80, WINDOW_HIGTH - 108, 100, 15)];
    [shapeMatchView setMeterType:DPMeterTypeLinearHorizontal];
    [shapeMatchView setShape:[UIBezierPath stars:5 shapeInFrame:shapeMatchView.frame].CGPath];
    shapeMatchView.progressTintColor = [UIColor colorWithRed:255/255.f green:199/255.f blue:87/255.f alpha:1.f];
    shapeMatchView.trackTintColor = [UIColor grayColor];
    [self.view addSubview:shapeMatchView];
    
    heartView = [[DPMeterView alloc] initWithFrame:CGRectMake(220, WINDOW_HIGTH - 152, 60, 60)];
    [heartView setShape:[UIBezierPath heartShape:heartView.frame].CGPath];
    heartView.progressTintColor = [UIColor colorWithRed:189/255.f green:32/255.f blue:49/255.f alpha:1.f];
    heartView.trackTintColor = [UIColor grayColor];
    [self.view addSubview:heartView];
    
    percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(228, WINDOW_HIGTH - 130, 50, 15)];
    percentLabel.backgroundColor = [UIColor clearColor];
    percentLabel.text = @"1\%";
    percentLabel.textColor = [UIColor whiteColor];
    percentLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:percentLabel];
    
    candleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 112, 50, 20)];
    candleLabel.backgroundColor = [UIColor clearColor];
    candleLabel.textColor = [UIColor whiteColor];
    candleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:candleLabel];
    
    coinLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 68, 50, 20)];
    coinLabel.backgroundColor = [UIColor clearColor];
    coinLabel.textColor = [UIColor whiteColor];
    coinLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:coinLabel];
    
    UILabel *lightlabel = [[UILabel alloc] initWithFrame:CGRectMake(60, WINDOW_HIGTH - 44, 40, 20)];
    lightlabel.backgroundColor = [UIColor clearColor];
    lightlabel.textColor = [UIColor whiteColor];
    lightlabel.textAlignment = UITextAlignmentCenter;
    lightlabel.text = @"点亮";
    [self.view addSubview:lightlabel];
    
    UILabel *giveUplabel = [[UILabel alloc] initWithFrame:CGRectMake(220, WINDOW_HIGTH - 44, 40, 20)];
    giveUplabel.backgroundColor = [UIColor clearColor];
    giveUplabel.textColor = [UIColor whiteColor];
    giveUplabel.textAlignment = UITextAlignmentCenter;
    giveUplabel.text = @"放弃";
    [self.view addSubview:giveUplabel];
    
}

#pragma mark -prive fun-
-(void)backBtnPress
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)lightbtnpress
{
    if([DataControl shareControl].TelepathyDS.strGetUser == nil ||
       [[DataControl shareControl].TelepathyDS.strGetUser isEqualToString:@""])
    {
        return;
    }
    
    iGameState = LVS_WAIT_LIGHT;
    iTimeCount = 0;
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在点亮..."
                                                      withView:self.view];
    
    [[netManager sharedNetManager].sendTelepL SendTelepL:1
                                                withMail:
     [DataControl shareControl].TelepathyDS.strGetUser];
}

-(void)giveupbtnpress
{
    [self cleanUI];
    
    iTimeCount = 0;
    iGameState = LVS_WAIT;
    bGetImage = false;
    bGetInfo = false;
    
    [[netManager sharedNetManager].sendTeleGetUser SendTelepGetUser];
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在寻找真心人"
                                                      withView:self.view];
}

-(void)cleanUI
{
    [shapeOppView setProgress:0.0f];
    [shapeMatchView setProgress:0.0f];
    [shapeStepView setProgress:0.0f];
    [heartView setProgress:0.0f];
    
    percentLabel.text = @"0\%";
    
    iMatch = 0;
    iStep = 0;
    iOpp = 0;
    ipercent = 0;
}

-(void)viewWillShow
{
    [[netManager sharedNetManager].sendTeleGetUser SendTelepGetUser];
    [[netManager sharedNetManager].sendTelepC sendTelepathyCandle];
    
    [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在寻找心动的人"
                                                      withView:self.view];
    
    iTimeCount = 0;
    iGameState = LVS_WAIT;
    bGetImage = false;
    bGetInfo = false;
    
    
    
    [self cleanUI];
}

-(void)refreshView
{
    if (iGameState != LVS_WAIT) {
        return;
    }
    
    NSString *strmail = [[DataControl shareControl].TelepathyDS GetUser];
    FirendInfo *fi = [[DataControl shareControl] SearchInfo:strmail];
    if (fi != nil && !bGetInfo) {
        bGetInfo = true;
        [self calculateMatch:fi];
    }
    
    BigImageInfo *biinfo = [[DataControl shareControl].bigHIDS SearchBigImageByMail:strmail];
    if (biinfo != nil && !bGetImage) {
        headview.image = [UIImage imageWithContentsOfFile:biinfo.url];
        
        bGetImage = true;
    }
    
    if (bGetImage && bGetInfo) {
        iGameState = LVS_START;
        
        [shapeStepView add:fabs((float)iStep / 10) animated:YES];
        [shapeOppView add:fabs((float)iOpp / 10) animated:YES];
        [shapeMatchView add:fabs((float)iMatch / 10) animated:YES];
        [heartView add:fabs((float)ipercent / 100) animated:YES];
        
        iTimeCount = 0;
        percentLabel.text = [NSString stringWithFormat:@"%d%@",ipercent,@"\%"];
        
        [[ViewManager sharedViewManager] dismissProgressHUD];

    }
}

-(void)calculateMatch:(FirendInfo *)fi
{
    if (fi == nil) {
        return;
    }
    
    if (abs(fi.age - [DataControl shareControl].personDS.uinfo.age) < 6) {
        iStep = arc4random() % 4 + 6;
    }
    else
    {
        iStep = arc4random() % 7 + 1;
    }
    
    if (fi.province == [DataControl shareControl].personDS.uinfo.province &&
        fi.city == [DataControl shareControl].personDS.uinfo.city) {
        iOpp = arc4random() % 4 + 6;
    }
    else if(fi.province == [DataControl shareControl].personDS.uinfo.province)
    {
        iOpp = arc4random() % 5 + 4;
    }
    else
    {
        iOpp = arc4random() % 7 + 1;
    }
    
    if (iOpp == iStep) {
        iMatch = iOpp;
    }
    else
    {
    int min = iOpp > iStep ? iStep:iOpp;
    iMatch = arc4random() % abs(iOpp - iStep) + min;
    }
    
        
    float ten = (iOpp + iStep) / 2;
    ipercent = ten * 10 + iMatch;
}

-(void)UICustomTick
{
    switch (iGameState) {
        case LVS_WAIT:
        {
            if (iTimeCount < 150) {
                iTimeCount++;
            }
            else
            {
                iTimeCount = 0;
                iGameState = LVS_START;
                
                bGetImage = false;
                bGetInfo = false;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
            }
        }
            break;
        case LVS_WAIT_LIGHT:
        {
            if (iTimeCount < 150) {
                iTimeCount++;
            }
            else
            {
                iTimeCount = 0;
                iGameState = LVS_START;
                
                bGetImage = false;
                bGetInfo = false;
                
                [[ViewManager sharedViewManager] dismissProgressHUD];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)CandleBack
{
    
    candleLabel.text = [NSString stringWithFormat:@"%d",
                        [DataControl shareControl].TelepathyDS.iCandle];
    
    float fprice = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
    int iprice = fprice / 10000;
    if (iprice > 0) {
        coinLabel.text = [NSString stringWithFormat:@"%d万",iprice];
    }
    else
    {
        coinLabel.text = [DataControl shareControl].personDS.uinfo.strcoin;
    }
}

-(void)LightBack
{
    iGameState = LVS_START;
    iTimeCount = 0;
    
    [[ViewManager sharedViewManager] dismissProgressHUD];
    
    candleLabel.text = [NSString stringWithFormat:@"%d",
                        [DataControl shareControl].TelepathyDS.iCandle];
    
    float fprice = [[DataControl shareControl].personDS.uinfo.strcoin floatValue];
    int iprice = fprice / 10000;
    if (iprice > 0) {
        coinLabel.text = [NSString stringWithFormat:@"%d万",iprice];
    }
    else
    {
        coinLabel.text = [DataControl shareControl].personDS.uinfo.strcoin;
    }
    
    switch ([DataControl shareControl].TelepathyDS.LightBackType) {
        case TELEP_LT_SUCCESS:
        {
            iAlertType = LVAT_LIGHT;
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"一见钟情"
                                                            message:
                                  @"点亮成功，对方将与你进行心电感应"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"下一个",nil];
            [alert show];
        }
            break;
        case TELEP_LT_CANDLE:
        {
            
        }
            break;
        case TELEP_LT_COIN:
        {
            iAlertType = LVAT_LIGHTFAIL;
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"一见钟情"
                                                            message:
                                  @"您的账户余额不足，您可以去每日任务领取蜡烛，或去充值!"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"充值",@"领蜡烛",nil];
            [alert show];
        }
            break;
        default:
            break;
    }
}

-(void)viewWillHide
{
    [self cleanUI];
}

#pragma mark -alertview delegate-
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        switch (iAlertType) {
            case LVAT_LIGHT:
            {
                [self cleanUI];
                
                iTimeCount = 0;
                iGameState = LVS_WAIT;
                bGetImage = false;
                bGetInfo = false;
                
                [[netManager sharedNetManager].sendTeleGetUser SendTelepGetUser];
                
                [[ViewManager sharedViewManager] showProgressHUDWithStatus:@"正在寻找真心人"
                                                                  withView:self.view];
            }
                break;
            case LVAT_LIGHTFAIL:
            {
                switch (buttonIndex) {
                    case 1:
                    {
                        if ([[UIDevice currentDevice] isJailbroken]) {
                            [[ViewManager sharedViewManager] showView:VI_RECHARGE
                                                     withIsRemoveCurV:false
                                                        withDirection:VD_RIGHT];
                            
                        }
                        else
                        {
                            [[ViewManager sharedViewManager] showView:VI_PURCHASE
                                                     withIsRemoveCurV:false
                                                        withDirection:VD_RIGHT];
                        }
                    }
                        break;
                        case 2:
                    {
                        
                    }
                        break;
                    default:
                        break;
                }
            }
                break;
            default:
                break;
        }
        
        
    }
}

@end
